|
| 1 | +import numpy as np |
| 2 | +from testUtils.codeGenerate import generateTestNetwork |
| 3 | +from testUtils.dmaUtils import MemcpyLayer, MemcpyParser, MemcpyTileConstraint, MemcpyTypeChecker, generate_graph, \ |
| 4 | + memcpyTemplate, prepare_deployer_with_custom_tiling, setup_snitch_deployer |
| 5 | +from testUtils.testRunner import TestRunner, TestRunnerArgumentParser |
| 6 | +from testUtils.typeMapping import baseTypeFromName, dtypeFromDeeployType |
| 7 | + |
| 8 | +from Deeploy.AbstractDataTypes import PointerClass |
| 9 | +from Deeploy.CommonExtensions.CodeTransformationPasses.MemoryAllocation import ArgumentStructGeneration, \ |
| 10 | + MemoryManagementGeneration |
| 11 | +from Deeploy.DeeployTypes import CodeTransformation, NodeBinding, NodeMapper |
| 12 | +from Deeploy.Targets.Snitch.Bindings import MemoryAwareFunctionCallClosure, TilingCallClosure |
| 13 | +from Deeploy.Targets.Snitch.CodeTransformationPasses import SnitchClusterTiling |
| 14 | +from Deeploy.Targets.Snitch.CodeTransformationPasses.SnitchClusterSynch import SnitchSynchCoresPass |
| 15 | +from Deeploy.Targets.Snitch.CodeTransformationPasses.SnitchCoreFilter import SnitchCoreFilterPass |
| 16 | +from Deeploy.Targets.Snitch.CodeTransformationPasses.SnitchProfileExecutionBlock import SnitchProfileExecutionBlockPass |
| 17 | +from Deeploy.Targets.Snitch.SnitchDma import SnitchDma |
| 18 | +from Deeploy.TilingExtension.CodeTransformationPasses.TilingVariableReplacement import TilingVariableReplacement, \ |
| 19 | + TilingVariableReplacementUpdate |
| 20 | +from Deeploy.TilingExtension.TilerExtension import TilingReadyNodeBindings |
| 21 | + |
| 22 | +testRunnerArgumentParser = TestRunnerArgumentParser(tiling_arguments = True) |
| 23 | +testRunnerArgumentParser.add_argument('--input-shape', |
| 24 | + nargs = '+', |
| 25 | + required = True, |
| 26 | + dest = 'input_shape', |
| 27 | + type = int, |
| 28 | + help = "Shape of the copied tensor") |
| 29 | +testRunnerArgumentParser.add_argument('--tile-shape', |
| 30 | + nargs = '+', |
| 31 | + required = True, |
| 32 | + dest = 'tile_shape', |
| 33 | + type = int, |
| 34 | + help = "Shape of the tiles produced in the manual tiling solution") |
| 35 | +testRunnerArgumentParser.add_argument('--node-count', |
| 36 | + dest = 'node_count', |
| 37 | + type = int, |
| 38 | + default = 1, |
| 39 | + help = "Number of generated memcpy nodes") |
| 40 | +testRunnerArgumentParser.add_argument('--type', type = str, default = "uint8_t", help = "Tensor elements datatype") |
| 41 | +testRunner = TestRunner('Snitch', 'gvsoc', tiling = True, argument_parser = testRunnerArgumentParser) |
| 42 | + |
| 43 | +inputShape = testRunner._args.input_shape |
| 44 | +tileShape = testRunner._args.tile_shape |
| 45 | +node_count = testRunner._args.node_count |
| 46 | +_type = baseTypeFromName(testRunner._args.type) |
| 47 | +dtype = dtypeFromDeeployType(_type) |
| 48 | +defaultMemory = "L2" |
| 49 | +targetMemory = "L1" |
| 50 | + |
| 51 | +assert len(inputShape) == len(tileShape), \ |
| 52 | + f'Input and tile shape should be of the same dimensionality. Received {len(inputShape)}D input shape vs. {len(tileShape)}D tile shape.' |
| 53 | +assert all(tileDim <= inDim for inDim, tileDim in zip(inputShape, tileShape)), \ |
| 54 | + f'Each tile shape dimension should be smaller then the corresponding input one. Received {tileShape} > {inputShape}' |
| 55 | + |
| 56 | +graph = generate_graph(node_count, inputShape, dtype) |
| 57 | +inputTypes = {"input_0": PointerClass(_type)} |
| 58 | +deployer = setup_snitch_deployer(defaultMemory, targetMemory, graph, inputTypes, testRunner._args.doublebuffer) |
| 59 | + |
| 60 | +transformer = CodeTransformation([ |
| 61 | + SnitchCoreFilterPass("compute"), |
| 62 | + SnitchProfileExecutionBlockPass(), |
| 63 | + TilingVariableReplacement(targetMemory), |
| 64 | + TilingCallClosure(writeback = False), |
| 65 | + SnitchSynchCoresPass(), |
| 66 | + TilingVariableReplacementUpdate(targetMemory), |
| 67 | + SnitchClusterTiling(defaultMemory, targetMemory, SnitchDma()), |
| 68 | + ArgumentStructGeneration(), |
| 69 | + MemoryManagementGeneration(targetMemory), |
| 70 | + MemoryAwareFunctionCallClosure(writeback = False, generateStruct = True), |
| 71 | + MemoryManagementGeneration(defaultMemory), |
| 72 | + MemoryManagementGeneration(), |
| 73 | +]) |
| 74 | + |
| 75 | +binding = NodeBinding(MemcpyTypeChecker(), memcpyTemplate, transformer) |
| 76 | +tilingReadyBindings = TilingReadyNodeBindings([binding], MemcpyTileConstraint()) |
| 77 | +memcpyMapper = NodeMapper(MemcpyParser(), tilingReadyBindings) |
| 78 | +memcpyMapping = {"Memcpy": MemcpyLayer([memcpyMapper])} |
| 79 | +deployer.Platform.engines[0].Mapping.update(memcpyMapping) |
| 80 | + |
| 81 | +prepare_deployer_with_custom_tiling(deployer, defaultMemory, targetMemory, tileShape, testRunner._args.doublebuffer) |
| 82 | + |
| 83 | +if not testRunner._args.skipgen: |
| 84 | + if dtype == np.float32: |
| 85 | + test_inputs = np.random.rand(*inputShape) |
| 86 | + else: |
| 87 | + info = np.iinfo(dtype) |
| 88 | + test_inputs = np.arange(stop = np.prod(inputShape), dtype = dtype).reshape(inputShape) |
| 89 | + test_outputs = test_inputs |
| 90 | + generateTestNetwork(deployer, [test_inputs], [test_outputs], inputTypes, testRunner._dir_gen) |
| 91 | + |
| 92 | +# Deconstructed testRunner.run() with skipped generation because we did the generation already |
| 93 | +testRunner.configure_cmake_project() |
| 94 | +testRunner.build_binary() |
| 95 | +if not testRunner._args.skipsim: |
| 96 | + testRunner.run_simulation() |
0 commit comments