|
| 1 | +// Copyright (c) 2023 Graphcore Ltd. All rights reserved. |
| 2 | +#define BOOST_TEST_MODULE init_tensor_offset_map |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <any> |
| 6 | +#include <boost/test/unit_test.hpp> |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <iostream> |
| 10 | +#include <map> |
| 11 | +#include <memory> |
| 12 | +#include <string> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
| 15 | +#include <poplar/Graph.hpp> |
| 16 | +#include <poplar/Interval.hpp> |
| 17 | + |
| 18 | +#include "popart/builder.gen.hpp" |
| 19 | +#include "popart/dataflow.hpp" |
| 20 | +#include "popart/debugcontext.hpp" |
| 21 | +#include "popart/inputshapeinfo.hpp" |
| 22 | +#include "popart/logging.hpp" |
| 23 | +#include "popart/names.hpp" |
| 24 | +#include "popart/patterns/patterns.hpp" |
| 25 | +#include "popart/sessionoptions.hpp" |
| 26 | +#include "popart/tensorinfo.hpp" |
| 27 | +#include "popart/util.hpp" |
| 28 | +#include "popart/voiddata.hpp" |
| 29 | + |
| 30 | +// This trick is required to access the Devicex's poplar::Tensors. |
| 31 | + |
| 32 | +#ifdef __clang__ |
| 33 | +#pragma clang diagnostic ignored "-Wkeyword-macro" |
| 34 | +#endif |
| 35 | +#define protected public |
| 36 | +#define private public |
| 37 | + |
| 38 | +#include <testdevice.hpp> |
| 39 | +#include <popart/builder.hpp> |
| 40 | +#include <popart/devicemanager.hpp> |
| 41 | +#include <popart/error.hpp> |
| 42 | +#include <popart/popx/devicex.hpp> |
| 43 | +#include <popart/popx/irlowering.hpp> |
| 44 | +#include <popart/session.hpp> |
| 45 | +#include <popart/sgd.hpp> |
| 46 | + |
| 47 | +#include "popart/popx/poptensors.hpp" |
| 48 | + |
| 49 | +#undef private |
| 50 | +#undef protected |
| 51 | + |
| 52 | +BOOST_AUTO_TEST_CASE(InitTensorOffsetMap) { |
| 53 | + // In this test, the input tensors are the exact size of a packet bytes for |
| 54 | + // one tile, Therefore, when createHostTransferableTensorWithOffset = true, |
| 55 | + // the accumulated tensor bytes is passed to createHostTransferableTensor() |
| 56 | + // as offset, and it mapping those tensors across tiles rather than mapping |
| 57 | + // them all to tile0. |
| 58 | + |
| 59 | + using namespace popart; |
| 60 | + |
| 61 | + auto builder = Builder::create(); |
| 62 | + auto aiOnnx = builder->aiOnnxOpset9(); |
| 63 | + |
| 64 | + // one packet per tile = 1024 bytes = 256 * FLOAT |
| 65 | + std::vector<int64_t> inputShape{1, 256}; |
| 66 | + TensorInfo inputInfo("FLOAT", inputShape); |
| 67 | + |
| 68 | + auto a = builder->addInputTensor( |
| 69 | + inputInfo, {TileSet::IO, ExchangeStrategy::OverlapInnerLoop}); |
| 70 | + auto b = builder->addInputTensor( |
| 71 | + inputInfo, {TileSet::IO, ExchangeStrategy::OverlapInnerLoop}); |
| 72 | + auto c = builder->addInputTensor( |
| 73 | + inputInfo, {TileSet::IO, ExchangeStrategy::OverlapInnerLoop}); |
| 74 | + auto x = aiOnnx.add({a, b}); |
| 75 | + x = aiOnnx.add({x, c}); |
| 76 | + builder->addOutputTensor(x); |
| 77 | + |
| 78 | + auto proto = builder->getModelProto(); |
| 79 | + auto dataFlow = DataFlow(5, {{x, AnchorReturnType("All")}}); |
| 80 | + |
| 81 | + SessionOptions opts; |
| 82 | + opts.virtualGraphMode = VirtualGraphMode::Auto; |
| 83 | + opts.enableExplicitMainLoops = true; |
| 84 | + opts.useHostCopyOps = true; |
| 85 | + opts.numIOTiles = 32; |
| 86 | + opts.experimentalSettings.createHostTransferableTensorWithOffset = true; |
| 87 | + |
| 88 | + auto device = createTestDevice(TEST_TARGET, 1); |
| 89 | + |
| 90 | + auto session = popart::InferenceSession::createFromOnnxModel( |
| 91 | + proto, |
| 92 | + dataFlow, |
| 93 | + device, |
| 94 | + InputShapeInfo(), |
| 95 | + opts, |
| 96 | + popart::Patterns(PatternsLevel::Default)); |
| 97 | + |
| 98 | + session->prepareDevice(); |
| 99 | + |
| 100 | + using Mapping = poplar::Graph::TileToTensorMapping; |
| 101 | + |
| 102 | + auto getStartTile = [&](const Mapping &ans) { |
| 103 | + unsigned index = 0; |
| 104 | + for (unsigned i = 0; i < ans.size(); ++i) { |
| 105 | + if (!ans[i].empty()) { |
| 106 | + index = i; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + return index; |
| 111 | + }; |
| 112 | + |
| 113 | + std::map<std::string, unsigned> startMappings; |
| 114 | + auto &irLowering = session->getDevice().lowering(); |
| 115 | + const auto &ir = irLowering.ir(); |
| 116 | + for (auto &id : ir.getAllTensorIds()) { |
| 117 | + auto *t = ir.getTensor(id); |
| 118 | + if (t->isHostLoadTensor()) { |
| 119 | + auto vgid = t->getVirtualGraphIdAndTileSetUnsafe(); |
| 120 | + auto &graph = irLowering.getVirtualGraph(vgid.first, vgid.second); |
| 121 | + auto &tensor = irLowering.tensors().get(t->id); |
| 122 | + const auto &tm = graph.getTileMapping(tensor); |
| 123 | + auto startTile = getStartTile(graph.getTileMapping(tensor)); |
| 124 | + startMappings[t->id] = startTile; |
| 125 | + std::cout << t->id << " : " << tm << std::endl; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + std::set<unsigned> uniqueMappings; |
| 130 | + for (const auto &mappings : startMappings) { |
| 131 | + BOOST_CHECK(uniqueMappings.insert(mappings.second).second == true); |
| 132 | + } |
| 133 | + BOOST_CHECK(uniqueMappings.size() == startMappings.size()); |
| 134 | +} |
0 commit comments