Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Region based memory management for mini ML

Region based memory management for mini ML

region推論の実装をMLのサブセットで行った際の発表資料
リポジトリはこちら:https://github.com/Everysick/region_based_mini_ml

TAKAMASA Saichi

January 12, 2017
Tweet

More Decks by TAKAMASA Saichi

Other Decks in Programming

Transcript

  1. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  2. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  3. ֓ཁ • Regionਪ࿦ • Regionʹؔ͢ΔจݙΛಡΉ • Mads Tofte, Jean-Pierre Talpin

    (1996)
 “Region-Based Memory Management” • Regionਪ࿦ΞϧΰϦζϜͷ࣮૷ • ࣮૷ͷΞΠσΞ΍ൃݟͨ͠఺Λ·ͱΊΔ • ಈ޲ͷௐࠪ • ML kit Ͱͷ࣮༻ྫ • ͦͷ΄͔طଘݴޠ΁ͷԠ༻ྫ
  4. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  5. Regionਪ࿦ʹ͍ͭͯ - RegionͷΞΠσΞ • ಈతʹϝϞϦ֬อ, ղ์͞ΕΔ"σʔλ"ʹ͍ͭͯɺ
 ͦͷ"σʔλ"͕ࢀর͞ΕΔྖҬΛRegionͱఆٛ • είʔϓ୯ҐͰRegionͷൣғ͸ܾఆ͞ΕΔ {

    int *ptr; { int x = 5; ptr = &x; } print (*ptr); } xͷείʔϓ(ෆਖ਼) είʔϓ୯Ґͷ؅ཧͰ͸ ͜ͷΑ͏ͳྫʹରͯ͠ରԠͰ͖ͳ͍
  6. Regionਪ࿦ʹ͍ͭͯ - Regionਪ࿦ͷ໨త • શͯͷRegionΛ࠷దʹਪ࿦Ͱ͖Ε͹, είʔϓΛ·͙ͨ "σʔλ"ͷ؅ཧΛશͯ੩తʹߦ͏͜ͱ͕Մೳ 㱺 Garbage Collection

    ͕ෆඞཁʹͳΔ 㱺 Stop the world ʹΑΔ࣮ߦ࣌ͷఀࢭ࣌ؒ࡟ݮͳͲ 㱺 ࣮ߦ࣌εϧʔϓοτͷ޲্ʹܨ͕Δ • ͨͩ͠Regionͷਪ࿦݁Ռʹ͸ྑ͠ѱ͕͋͠Δ • શͯάϩʔόϧͳRegionʹׂΓ౰ͯΔ͜ͱ΋Մೳ
  7. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • "Regionਪ࿦" ͸ "Region஫ऍ෇͖ܕͷܕਪ࿦" • ਪ࿦ʹΑͬͯRegionͷಋೖ, ඥ෇͚Λܾఆ͢Δ͜ͱ͕Մೳ

    • Regionਪ࿦͸ϓϩάϥϛϯάݴޠML΁ͷΞΠσΞ • આ໌ɺ࣮૷ͷλʔήοτʹMLΛ࠾༻ • ҎԼͷΑ͏ʹͯ͠MLͷߏจ΁RegionΛఆٛ letregion r in … end 1 @ r (λ x. x) @ r ɾRegion "r" ͷಋೖ "r" ʹඥ෇͘ "σʔλ" ͸ ͜ͷείʔϓ಺ͰͷΈ࢖༻͞ΕΔ ɾRegion "r" ΁ͷඥ෇͚ "σʔλ"ΛͲͷRegion΁ ඥ෇͚Δ͔Λ໌ࣔ
  8. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • Regionͷ৘ใ͕ແ͍ݴޠ͔ΒRegion෇͖ͷݴޠ΁ • ݩʹͳΔݴޠ͸७ਮͳMLͰྑ͍ • نଇʹैͬͯܕਪ࿦ͱஔ͖׵͑Λద༻͢Δ let

    x = (2, 3) in (λ y. (#1 x, y) end 5 letregion r4, r5 in letregion r6 in let x = (2 @ r2, 3 @ r6) @ r4 in (λ y. (#1 x, y) @ r1) @ r5 end end 5 @ r3 end
  9. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • MLͷܕΛ֦ு • Regionͷࣝผࢠ, Region΁ͷ࡞༻ͳͲͷ஫ऍΛ௥Ճ • e.g.

    (int) ! ((int, r1), {put(r1), get(r2)}) • ֦ு͞Εͨܕʹରͯ͠ܕਪ࿦Λߦ͏ • Damas and Milner (1982) ͷ MLtype, ML type scheme Λ֦ு • Regionਪ࿦ͷੑೳ͸ܕͷ୯ҰԽʹؼண • ୯ҰԽͷΞϧΰϦζϜʹΑͬͯ͸
 ඇޮ཰తͳRegionʹ"σʔλ"͕഑ஔ͞Εͯ͠·͏
  10. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  11. ࣮༻ྫ • ML kit • Standard MLॲཧܥ • Regionਪ࿦Λಋೖ͠ϝϞϦ؅ཧΛ΄΅RegionͰߦ͑Δ •

    ׬શͳGC less ͸೉͍͠ • ୯ҰԽΞϧΰϦζϜ͸Regionͷಉఆ͕Մೳͳ͜ͱ͕ূ໌͞Ε͍ͯΔ let fib n in if n < 2 then 1 else fib(n-1) + fib(n-2) end 10000…000 ɾ࠶ؼਂ౓͕ਂ͍ ɾRegionͷղ์ʹͨͲΓண͚ͳ͍ ɾෆཁͳϝϞϦ͕࠶ؼதʹ࢒Δ ɾղফͷͨΊGCΛಋೖ ɾRegion಺ͷϝϞϦΛ࠶׆༻͢Δ
  12. ࣮༻ྫ • Rust (Cyclone) • Cyclone͸CݴޠͷγϯλοΫεʹRegionͷ֓೦Λಋೖ • ಈతͳRegion, ੩తͳRegion, ώʔϓΛ࢖ͬͨRegion

    • Regionਪ࿦ͱ͸ҟͳΔ΋ͷ • Cycloneͱͯ͠ͷ։ൃ͸ऴྃ • Cyclone͕Rust΁ٵऩ͞Ε৽نݴޠͱͯ͠։ൃ͕ਐߦ • Ownership΍Lifetime(≒ Region)ͳͲͷ֓೦Λಋೖ • Regionਪ࿦Ͱ͸ͳ͘RegionΛಋೖͨ͠GC lessͷݴޠ
  13. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  14. ·ͱΊ, ՝୊ • ·ͱΊ • RegionɺRegionਪ࿦ʹֶ͍ͭͯΜͩ • Regionਪ࿦ ≒ ୯ҰԽʹґଘ͢Δ͜ͱΛཧղͨ͠

    • Regionਪ࿦ͷ࣮૷Λߦͬͨ • ਪ࿦෦෼500ߦఔ౓, MLͷαϒηοτͰಈ࡞ • Region׆༻ྫʹ͍ͭͯͷαʔϕΠΛߦͬͨ • ՝୊ • ࣮૷ͨ͠ϓϩάϥϜͷվળ • ਪ࿦Ҏ֎ͷRegionͷѻ͍ʹ͍ͭͯͷௐࠪ
  15. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  16. ࢀߟจݙ • Mads Tofte, Jean-Pierre Talpin (1996)
 “Region-Based Memory Management”

    • Mads Tofte, Lars Birkedal (1998)
 “A region inference algorithm” • Dan Grossman, Greg Morrisett, Trevor Jim, 
 Michael Hicks, Yanling Wang, James Cheney(2002) 
 “Region-based memory management in cyclone” • ถాڡ࢙, ӏ઒࢝ཅ, ՖҪ ྄, ീਿণ޺, ౬ᕆଠҰ(2005)
 ”Ϧʔδϣϯม਺ͷಈతͳΤΠϦΞε൑ఆʹΑΔϝϞϦޮ཰޲্”