Archive native training 2026-10-08 manifest 55e57f14c331c9a0a4cab71d6c237d5436e691a2f2862b2734844db400e284c8
b60a0ad verified Download native_training_20261008/native/cpp/candidate/exact_atlas.cpp from TryDotAtwo/quantum-generator: direct link, hf CLI and curl.
- Browser
- Download file 6.36 kB
-
https://huggingface.co/datasets/TryDotAtwo/quantum-generator/resolve/main/native_training_20261008/native/cpp/candidate/exact_atlas.cpp
- Command line
-
hf download hf://datasets/TryDotAtwo/quantum-generator/native_training_20261008/native/cpp/candidate/exact_atlas.cpp
-
curl -L -o exact_atlas.cpp https://huggingface.co/datasets/TryDotAtwo/quantum-generator/resolve/main/native_training_20261008/native/cpp/candidate/exact_atlas.cpp
6.36 kB
| // BFS ordering is part of the Python reference's first-minimum tie contract. | |
| struct State { | |
| std::array<uint8_t,16> map{}; | |
| std::array<uint8_t,16> populations{}; | |
| int32_t parent=-1; | |
| uint16_t distance=0, control=0, target=0; | |
| }; | |
| struct Key { | |
| uint64_t value; | |
| bool operator==(const Key& other) const { return value==other.value; } | |
| }; | |
| struct Hash { size_t operator()(Key key) const { return std::hash<uint64_t>{}(key.value); } }; | |
| static Key key(const State& state, unsigned count) { | |
| uint64_t result=0; | |
| for(unsigned i=0;i<count;++i) result|=uint64_t(state.map[i])<<(4*i); | |
| return {result}; | |
| } | |
| // Immutable table export for CUDA scoring. Codes are the contiguous dimension. | |
| static unsigned population(unsigned value); | |
| static const std::vector<State>& atlas(unsigned width,unsigned side); | |
| extern "C" API int32_t frontier_atlas_table(uint32_t width,uint32_t side, | |
| uint8_t* populations,uint16_t* distances,uint32_t state_capacity,uint32_t* states_count) noexcept { | |
| if(width<1||width>4||side>1||!states_count) return 1; | |
| try { | |
| const auto& states=atlas(width,side);*states_count=uint32_t(states.size()); | |
| if(!populations||!distances||state_capacity<states.size()) return 2; | |
| for(size_t j=0;j<states.size();++j) { | |
| distances[j]=states[j].distance; | |
| for(unsigned i=0;i<(1u<<width);++i) populations[j*(1u<<width)+i]=uint8_t(population(states[j].map[i])); | |
| } | |
| return 0; | |
| } catch(...) {return 3;} | |
| } | |
| static std::array<std::vector<State>,8> tables; | |
| static std::array<std::once_flag,8> flags; | |
| // Full-histogram memoization preserves exhaustive scoring and first-minimum | |
| // choices. A bounded per-thread cache avoids repeated atlas scans across the | |
| // complete compiler's overlapping macro windows; clearing affects speed only. | |
| struct HistogramKey { | |
| std::array<uint32_t,16> histogram{}; | |
| uint32_t slot=0; | |
| bool operator==(const HistogramKey& other) const {return slot==other.slot&&histogram==other.histogram;} | |
| }; | |
| struct HistogramHash { | |
| size_t operator()(const HistogramKey& value) const { | |
| size_t hash=value.slot+1; | |
| for(auto entry:value.histogram)hash^=size_t(entry)+size_t(0x9e3779b9)+(hash<<6)+(hash>>2); | |
| return hash; | |
| } | |
| }; | |
| struct HistogramResult {uint64_t cost;uint32_t winner;}; | |
| static thread_local std::unordered_map<HistogramKey,HistogramResult,HistogramHash> histogram_cache; | |
| static thread_local uint64_t histogram_hits=0,histogram_misses=0; | |
| extern "C" API void frontier_atlas_cache_stats(uint64_t* hits,uint64_t* misses,uint32_t* entries) noexcept { | |
| if(hits)*hits=histogram_hits;if(misses)*misses=histogram_misses;if(entries)*entries=uint32_t(histogram_cache.size()); | |
| } | |
| static unsigned population(unsigned value) { | |
| unsigned n=0; for(;value;value>>=1) n+=value&1; return n; | |
| } | |
| static const std::vector<State>& atlas(unsigned width,unsigned side) { | |
| unsigned slot=(width-1)*2+side; | |
| std::call_once(flags[slot],[=] { | |
| auto& states=tables[slot]; | |
| unsigned count=1u<<width; | |
| State identity; for(unsigned i=0;i<count;++i) {identity.map[i]=uint8_t(i);identity.populations[i]=uint8_t(population(i));} | |
| states.push_back(identity); | |
| std::unordered_map<Key,uint32_t,Hash> seen; | |
| seen.emplace(key(identity,count),0); | |
| for(size_t index=0;index<states.size();++index) { | |
| State source=states[index]; | |
| for(unsigned c=0;c<width;++c) for(unsigned t=0;t<width;++t) { | |
| if(c==t) continue; | |
| State next=source; | |
| for(unsigned i=0;i<count;++i) { | |
| unsigned code=source.map[i]; | |
| next.map[i]=uint8_t(side==0 ? code^(((code>>t)&1)<<c) : code^(((code>>c)&1)<<t)); | |
| next.populations[i]=uint8_t(population(next.map[i])); | |
| } | |
| auto inserted=seen.emplace(key(next,count),uint32_t(states.size())); | |
| if(!inserted.second) continue; | |
| next.parent=int32_t(index); next.distance=source.distance+1; | |
| next.control=uint16_t(c);next.target=uint16_t(t); | |
| states.push_back(next); | |
| } | |
| } | |
| }); | |
| return tables[slot]; | |
| } | |
| extern "C" API int32_t frontier_atlas_optimize( | |
| uint32_t width,uint32_t side,const uint32_t* histogram,uint32_t histogram_count, | |
| uint16_t* controls,uint16_t* targets,uint32_t capacity, | |
| uint32_t* length,uint64_t* cost,uint32_t* selected,uint32_t* states_count) noexcept { | |
| if(width<1||width>4||side>1||histogram_count!=(1u<<width)||!histogram|| | |
| !length||!cost||!selected||!states_count||!controls||!targets) return 1; | |
| try { | |
| const auto& states=atlas(width,side); | |
| uint64_t best=std::numeric_limits<uint64_t>::max();uint32_t winner=0; | |
| HistogramKey cache_key;cache_key.slot=(width-1)*2+side; | |
| std::copy(histogram,histogram+histogram_count,cache_key.histogram.begin()); | |
| auto cached=histogram_cache.find(cache_key); | |
| if(cached!=histogram_cache.end()) { | |
| ++histogram_hits;best=cached->second.cost;winner=cached->second.winner; | |
| } else { | |
| ++histogram_misses; | |
| for(uint32_t j=0;j<states.size();++j) { | |
| uint64_t candidate=2u*states[j].distance; | |
| for(unsigned i=0;i<histogram_count;++i) | |
| candidate+=uint64_t(states[j].populations[i])*histogram[i]; | |
| if(candidate<best) {best=candidate;winner=j;} | |
| } | |
| if(histogram_cache.size()>=65536)histogram_cache.clear(); | |
| histogram_cache.emplace(cache_key,HistogramResult{best,winner}); | |
| } | |
| *length=states[winner].distance;*cost=best;*selected=winner;*states_count=uint32_t(states.size()); | |
| if(*length>capacity) return 2; | |
| uint32_t current=winner; | |
| for(uint32_t remaining=*length;remaining;--remaining) { | |
| controls[remaining-1]=states[current].control; | |
| targets[remaining-1]=states[current].target; | |
| current=uint32_t(states[current].parent); | |
| } | |
| return 0; | |
| } catch(...) {return 3;} | |
| } | |