|
| 1 | +# Device Capability Seam |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The device capability seam extracts GPU capability queries from the singleton pattern (`DeviceInfoCache::instance()`), allowing tests to inject fake device info without requiring specific GPU hardware. |
| 6 | + |
| 7 | +## Design |
| 8 | + |
| 9 | +### Core Interface |
| 10 | + |
| 11 | +`DeviceInfoProvider` (in `src/utils/device_info_provider.cuh`): |
| 12 | +- Lightweight struct-based interface (no virtual dispatch overhead) |
| 13 | +- Provides minimal surface for querying GPU capabilities |
| 14 | +- Methods: `hasTensorCores()`, `computeMajor()`, `computeMinor()`, `smCount()`, etc. |
| 15 | + |
| 16 | +### Production Adapter |
| 17 | + |
| 18 | +`ProductionDeviceInfoProvider`: |
| 19 | +- Backed by `DeviceInfoCache` singleton |
| 20 | +- Used by default in all production code paths |
| 21 | +- Zero runtime overhead compared to direct singleton access |
| 22 | + |
| 23 | +### API Pattern |
| 24 | + |
| 25 | +All capability-dependent functions provide two overloads: |
| 26 | + |
| 27 | +```cpp |
| 28 | +// With explicit provider (for tests) |
| 29 | +bool tensorCoresAvailable(const DeviceInfoProvider &provider); |
| 30 | +float getTheoreticalPeakGflops(const DeviceInfoProvider &provider); |
| 31 | + |
| 32 | +// Default (uses production provider) |
| 33 | +bool tensorCoresAvailable(); |
| 34 | +float getTheoreticalPeakGflops(); |
| 35 | +``` |
| 36 | +
|
| 37 | +## Usage |
| 38 | +
|
| 39 | +### Production Code |
| 40 | +
|
| 41 | +Production code continues to use no-argument versions: |
| 42 | +
|
| 43 | +```cpp |
| 44 | +if (tensorCoresAvailable()) { |
| 45 | + // Use Tensor Cores |
| 46 | +} |
| 47 | +float peak = getTheoreticalPeakGflops(); |
| 48 | +``` |
| 49 | + |
| 50 | +### Test Code |
| 51 | + |
| 52 | +Tests can inject fake device info: |
| 53 | + |
| 54 | +```cpp |
| 55 | +cudaDeviceProp fake_prop{}; |
| 56 | +fake_prop.major = 7; // Volta |
| 57 | +fake_prop.minor = 0; |
| 58 | +fake_prop.multiProcessorCount = 80; |
| 59 | + |
| 60 | +DeviceInfoProvider fake_provider{&fake_prop, 64, 1.53f}; |
| 61 | + |
| 62 | +EXPECT_TRUE(tensorCoresAvailable(fake_provider)); |
| 63 | +EXPECT_NEAR(getTheoreticalPeakGflops(fake_provider), 15667.2f, 0.1f); |
| 64 | +``` |
| 65 | +
|
| 66 | +## Affected Modules |
| 67 | +
|
| 68 | +### `src/utils/cuda_utils.cuh` |
| 69 | +- Added production adapter implementation |
| 70 | +- `DeviceInfoCache` remains unchanged (still singleton) |
| 71 | +- Only the production adapter calls the singleton |
| 72 | +
|
| 73 | +### `src/kernels/tensor_core_sgemm.cuh` |
| 74 | +- `tensorCoresAvailable()` - overloaded |
| 75 | +- `getTensorCoreArchName()` - overloaded |
| 76 | +- No longer calls `DeviceInfoCache::instance()` directly |
| 77 | +
|
| 78 | +### `src/utils/benchmark_metrics.cuh` |
| 79 | +- `getTheoreticalPeakGflops()` - overloaded |
| 80 | +- `getTheoreticalPeakBandwidth()` - overloaded |
| 81 | +- No longer calls `DeviceInfoCache::instance()` directly |
| 82 | +
|
| 83 | +## Testing |
| 84 | +
|
| 85 | +See `tests/test_device_info_seam.cu` for comprehensive examples: |
| 86 | +
|
| 87 | +- Fake device provider creation |
| 88 | +- Tensor Core capability testing without real hardware |
| 89 | +- Peak GFLOPS/bandwidth calculations with fake devices |
| 90 | +- Architectural classification (Volta, Ampere, Hopper, etc.) |
| 91 | +- Production adapter integration tests |
| 92 | +
|
| 93 | +## Design Rationale |
| 94 | +
|
| 95 | +### Why struct-based instead of virtual interfaces? |
| 96 | +
|
| 97 | +- Zero runtime overhead (no vtable lookups) |
| 98 | +- Header-only implementation remains simple |
| 99 | +- Sufficient for the single use case (production vs test) |
| 100 | +
|
| 101 | +### Why preserve the singleton? |
| 102 | +
|
| 103 | +- Minimal code changes to existing production paths |
| 104 | +- The singleton itself isn't problematic; the hard dependency was |
| 105 | +- Production adapter provides the indirection we need |
| 106 | +
|
| 107 | +### Why overloads instead of default parameters? |
| 108 | +
|
| 109 | +- Clearer call sites in tests |
| 110 | +- No ambiguity about which version is being called |
| 111 | +- Easier to grep for test-specific vs production usage |
| 112 | +
|
| 113 | +## Limitations |
| 114 | +
|
| 115 | +- Tests still require a CUDA-capable GPU to run (for initialization) |
| 116 | +- Only capability queries are abstracted, not actual kernel execution |
| 117 | +- The seam is focused on decision logic, not full GPU emulation |
| 118 | +
|
| 119 | +## Future Extensions |
| 120 | +
|
| 121 | +If needed, this seam could be extended to support: |
| 122 | +- Mock kernel execution for unit tests |
| 123 | +- Multiple device scenarios in a single test |
| 124 | +- Device capability fuzzing for edge case discovery |
0 commit comments