@@ -186,42 +186,49 @@ void session_register_hooks(uint64_t id, uintptr_t module_ptr,
186186 SessionState* state = SessionState::get (id);
187187 if (!state) return ;
188188
189- // 1) Create per-layer config
190- auto & cfg = state->layer_configs [layer_key];
191- if (!cfg) cfg = std::make_shared<LayerHookConfig>();
192- cfg->capture_dir = static_cast <CaptureDir>(capture_dir_int);
189+ auto make_config = [&](const std::string& key) -> std::shared_ptr<LayerHookConfig> {
190+ auto & cfg = state->layer_configs [key];
191+ if (!cfg) cfg = std::make_shared<LayerHookConfig>();
192+ cfg->capture_dir = static_cast <CaptureDir>(capture_dir_int);
193+ CapturePolicy cap = CapturePolicy::EVERY ;
194+ if (state->max_batches > 0 ) cap = CapturePolicy::MAX_K ;
195+ else if (state->sample_every > 1 ) cap = CapturePolicy::SAMPLE_N ;
196+ cfg->counter .policy = cap;
197+ cfg->counter .sample_every = state->sample_every ;
198+ cfg->counter .max_batches = state->max_batches ;
199+ if (!reduction_path.empty ())
200+ cfg->reduction = std::make_shared<Reduction>(reduction_path);
201+ return cfg;
202+ };
193203
194- CapturePolicy cap = CapturePolicy::EVERY ;
195- if (state->max_batches > 0 ) cap = CapturePolicy::MAX_K ;
196- else if (state->sample_every > 1 ) cap = CapturePolicy::SAMPLE_N ;
197- cfg->counter .policy = cap;
198- cfg->counter .sample_every = state->sample_every ;
199- cfg->counter .max_batches = state->max_batches ;
204+ auto make_accum = [&](const std::string& key) -> std::shared_ptr<LayerAccumulator> {
205+ std::lock_guard<std::mutex> lock (state->mutex );
206+ auto it = state->accum_data .find (key);
207+ if (it != state->accum_data .end ()) return it->second ;
208+ auto a = std::make_shared<LayerAccumulator>();
209+ state->accum_data [key] = a;
210+ return a;
211+ };
200212
201- // 2) Load reduction from .pt file if non-empty
202- if (!reduction_path.empty ()) {
203- cfg->reduction = std::make_shared<Reduction>(reduction_path);
204- }
213+ CaptureDir dir = static_cast <CaptureDir>(capture_dir_int);
214+ py::gil_scoped_acquire gil;
205215
206- // 3) Create or reuse shared accumulator (pre-seeded by session_init_accumulator)
207- std::shared_ptr<LayerAccumulator> accum;
208- {
209- std::lock_guard<std::mutex> lock (state->mutex );
210- auto it = state->accum_data .find (layer_key);
211- if (it != state->accum_data .end ()) {
212- accum = it->second ; // reuse pre-seeded accumulator
213- } else {
214- accum = std::make_shared<LayerAccumulator>();
215- state->accum_data [layer_key] = accum;
216- }
216+ if (dir == CaptureDir::OUTPUT || dir == CaptureDir::BOTH ) {
217+ std::string out_key = layer_key + " .output" ;
218+ make_config (out_key);
219+ auto accum = make_accum (out_key);
220+ register_hooks_on_module (
221+ reinterpret_cast <void *>(module_ptr), state, out_key,
222+ static_cast <int32_t >(CaptureDir::OUTPUT ), accum);
217223 }
218224
219- // 4) Register hooks on module (GIL required for pybind11 call)
220- {
221- py::gil_scoped_acquire gil;
225+ if (dir == CaptureDir::INPUT || dir == CaptureDir::BOTH ) {
226+ std::string in_key = layer_key + " .input" ;
227+ make_config (in_key);
228+ auto accum = make_accum (in_key);
222229 register_hooks_on_module (
223- reinterpret_cast <void *>(module_ptr), state, layer_key ,
224- capture_dir_int , accum);
230+ reinterpret_cast <void *>(module_ptr), state, in_key ,
231+ static_cast < int32_t >(CaptureDir:: INPUT ) , accum);
225232 }
226233}
227234
0 commit comments