Skip to content

MDMM hardware-aware metrics (FPGA + PACA), tests and docs#46

Open
ArghyaRanjanDas wants to merge 3 commits into
cern-nextgen:devfrom
ArghyaRanjanDas:add_hardware_aware_metrics
Open

MDMM hardware-aware metrics (FPGA + PACA), tests and docs#46
ArghyaRanjanDas wants to merge 3 commits into
cern-nextgen:devfrom
ArghyaRanjanDas:add_hardware_aware_metrics

Conversation

@ArghyaRanjanDas

Copy link
Copy Markdown
Contributor

Summary

  • Add FPGAAwareSparsity and PACAPattern MDMM metrics to both the Keras and Torch backends, ported onto dev's per-backend layout. Supersedes MDMM tests and hardware-aware metric functions #37, which was built on the old single pruning_methods/ tree and targeted main.
  • Fix the Pydantic serialization warnings reported on MDMM tests and hardware-aware metric functions #37 (the constraint_type enum and the discriminated pruning_parameters union). Root cause: the MDMM enum fields defaulted to bare strings, and Pydantic does not validate/coerce defaults, so the runtime value stayed a str. They now default to enum members.
  • Move metric validation into MDMMPruningModel (ge/le + Literal) and drop the per-metric asserts. PACA auto-forces Equality/target 0 via a model_validator, so the MDMM layer stays metric-agnostic.
  • Run pattern selection on-graph (keras.ops bit-pack + bincount, native torch.unique, no NumPy round-trip of the weight); centralise distance/layout/resource constants in core/constants.py; fix the get_layer_sparsity float/int dtype bug on the TF backend.
  • Add config_mdmm_fpga.yaml, config_mdmm_paca.yaml, the mdmm_fpga_config() / mdmm_paca_config() helpers, and document the new metric types in reference.md and getting_started.md.

Test plan

  • cd tests && pytest test_mdmm.py test_mdmm_metrics.py (Keras / TensorFlow)
  • cd tests && KERAS_BACKEND=torch pytest test_mdmm.py test_mdmm_metrics.py (Keras / Torch)
  • cd tests && KERAS_BACKEND=torch pytest test_torch_mdmm.py (native Torch)
  • Load config_mdmm_fpga.yaml and config_mdmm_paca.yaml, call model_dump(), confirm no PydanticSerializationUnexpectedValue warnings.
  • Existing methods unaffected: pytest test_pdp.py test_ap.py.

Notes for review

  • Most of the MDMM tests and hardware-aware metric functions #37 inline comments are already satisfied by dev's restructure: metric_type is read off the config and resolved via the metric registry, and the MetricType / ConstraintType enums already exist. This PR keeps that flat-params + registry approach rather than nested metric models.
  • The patterns.py no-op OIHW->OIHW permute is replaced with real per-backend layout handling (Keras kernels are HWIO, Torch OIHW), and the convert_to_numpy pattern step is gone.
  • The shipped FPGA config uses Equality/target 0; happy to change the default constraint if you prefer a different target for structured group-sparsity.

Add the two hardware-aware sparsity metrics for MDMM to both the Keras and Torch
backends, on dev's per-backend layout:

- FPGAAwareSparsityMetric: fraction of zero DSP/BRAM weight groups, modelling FPGA
  resource packing (rf / precision / bram_width).
- PACAPatternMetric: drives conv kernels onto a small set of dominant binary
  patterns. Pattern selection stays on-graph (keras.ops bit-pack + bincount /
  native torch.unique, no NumPy round-trip of the weight) and exposes a projection
  mask applied during fine-tuning through a generic, metric-agnostic hook in MDMM.

Data model (MDMMPruningModel):
- New metric_type values + flat Optional fields (precision, target_resource,
  bram_width, num_patterns_to_keep, beta, distance_metric) validated by Pydantic
  (ge/le + Literal); a model_validator forces PACA -> Equality / target 0.
- Fix the reported Pydantic serialization warnings (constraint_type 'Equality' and
  the discriminated pruning_parameters union): enum fields now default to enum
  members instead of bare strings, which Pydantic does not coerce on defaults.

Also: distance/layout/resource constants centralised in core/constants.py, the
get_layer_sparsity float/int dtype fix on the TF backend, and config_mdmm_fpga.yaml
+ config_mdmm_paca.yaml with mdmm_fpga_config() / mdmm_paca_config() helpers.
- test_mdmm.py / test_mdmm_metrics.py: Keras MDMM, FPGA DSP/BRAM math, PACA pattern
  selection + projection masking, full phase cycles, Pydantic validation, and a
  regression test that MDMM configs serialize without Pydantic warnings.
- test_torch_mdmm.py: native-torch MDMM + metrics.
- run_tests.sh: run the new files under both tensorflow and torch backends.
- reference.md: MDMM metric-type overview + FPGAAwareSparsity and PACAPatternSparsity
  parameter tables.
- getting_started.md: constraint-based MDMM usage with the hardware-aware configs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant