|
| 1 | +// Copyright (c) 2019 Graphcore Ltd. All rights reserved. |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <popart/names.hpp> |
| 5 | +#include <popart/op.hpp> |
| 6 | +#include <popart/opmanager.hpp> |
| 7 | +#include <popart/popx/devicex.hpp> |
| 8 | +#include <popart/popx/opx.hpp> |
| 9 | +#include <popart/popx/opxmanager.hpp> |
| 10 | +#include <popart/region.hpp> |
| 11 | +#include <popart/shapeinference.hpp> |
| 12 | +#include <popops/Cast.hpp> |
| 13 | +#include <popops/ElementWise.hpp> |
| 14 | +#include <popops/Rearrange.hpp> |
| 15 | +#include <poputil/TileMapping.hpp> |
| 16 | +#include <random> |
| 17 | + |
| 18 | +using namespace popart; |
| 19 | +using namespace popart::popx; |
| 20 | +using namespace popops::expr; |
| 21 | + |
| 22 | +namespace CustomOperators { |
| 23 | +const popart::OperatorIdentifier AttentionMask = {"ai.graphcore", |
| 24 | + "AttentionMask", 1}; |
| 25 | +} // namespace CustomOperators |
| 26 | + |
| 27 | +// An InplaceIdentityOp that doesn't return any grad ops. This allows you to |
| 28 | +// disconnect the flow of gradients when creating the backwards pass |
| 29 | +class AttentionMaskOp : public popart::Op { |
| 30 | + public: |
| 31 | + poplar::Type dataType; |
| 32 | + |
| 33 | + AttentionMaskOp(const popart::OperatorIdentifier& _opid, |
| 34 | + const Op::Settings& settings_, poplar::Type& dataTypeIn) |
| 35 | + : Op(_opid, settings_), dataType(dataTypeIn) {} |
| 36 | + |
| 37 | + void setup() final { |
| 38 | + // input shape [B, S] |
| 39 | + Shape inShape = inInfo(0).shape(); |
| 40 | + Shape refShape = inInfo(1).shape(); |
| 41 | + |
| 42 | + // output shape [B, 1, S, S] |
| 43 | + Shape outShape = {inShape.at(0), 1, inShape.at(1), inShape.at(1)}; |
| 44 | + |
| 45 | + if (dataType == poplar::HALF) |
| 46 | + outInfo(0) = {"FLOAT16", outShape}; |
| 47 | + else |
| 48 | + outInfo(0) = {"FLOAT", outShape}; |
| 49 | + } |
| 50 | + |
| 51 | + std::unique_ptr<Op> clone() const final { |
| 52 | + return std::make_unique<AttentionMaskOp>(*this); |
| 53 | + } |
| 54 | + |
| 55 | + float getSubgraphValue() const final { return getLowSubgraphValue(); } |
| 56 | +}; |
| 57 | + |
| 58 | +static popart::OpDefinition attentionMaskOpDef({}); |
| 59 | + |
| 60 | +static popart::OpCreator<AttentionMaskOp> attentionMaskOpCreator( |
| 61 | + popart::OpDefinitions({{CustomOperators::AttentionMask, |
| 62 | + attentionMaskOpDef}}), |
| 63 | + [](const popart::OpCreatorInfo& oci) -> std::unique_ptr<popart::Op> { |
| 64 | + std::string type = |
| 65 | + oci.attributes.getAttribute<Attributes::String>("dataType"); |
| 66 | + poplar::Type dataType = (type == "FLOAT") ? poplar::FLOAT : poplar::HALF; |
| 67 | + |
| 68 | + return std::unique_ptr<AttentionMaskOp>( |
| 69 | + new AttentionMaskOp(oci.opid, oci.settings, dataType)); |
| 70 | + }, |
| 71 | + true); |
| 72 | + |
| 73 | +class AttentionMaskOpX : public popart::popx::Opx { |
| 74 | + public: |
| 75 | + AttentionMaskOpX(popart::Op* op, popart::popx::Devicex* devicex) |
| 76 | + : popart::popx::Opx(op, devicex) { |
| 77 | + verifyOp<AttentionMaskOp>(op, CustomOperators::AttentionMask); |
| 78 | + } |
| 79 | + |
| 80 | + popart::popx::InputCreatorType getInputCreatorType(popart::InIndex) const { |
| 81 | + return popart::popx::InputCreatorType::CanUnwind; |
| 82 | + } |
| 83 | + |
| 84 | + poplar::Tensor unwindTensorLayout(poplar::Tensor tensor, popart::InIndex, |
| 85 | + popart::OutIndex) const { |
| 86 | + return tensor; |
| 87 | + } |
| 88 | + |
| 89 | + popart::view::RegMap unwindRegion(popart::InIndex, popart::OutIndex) const { |
| 90 | + return [this](const popart::view::Region& r) { |
| 91 | + return popart::view::Regions(1, r); |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + void grow(poplar::program::Sequence& prog) const final { |
| 96 | + AttentionMaskOp& myOp = getOp<AttentionMaskOp>(); |
| 97 | + |
| 98 | + poplar::Type dataType = myOp.dataType; |
| 99 | + poplar::Graph& graph = Opx::graph(); |
| 100 | + // input tensor shape [B, S] |
| 101 | + poplar::Tensor seqIndex = getInTensor(0); |
| 102 | + std::size_t batchSize = seqIndex.dim(0); |
| 103 | + std::size_t seqLength = seqIndex.dim(1); |
| 104 | + seqIndex = seqIndex.reshape({batchSize, seqLength, 1}); |
| 105 | + seqIndex = popops::cast(graph, seqIndex, dataType, prog, "input_mask_f"); |
| 106 | + poplar::Tensor attentionMatrix = getInTensor(1); |
| 107 | + |
| 108 | + const auto dimOrdering = |
| 109 | + poputil::detectDimGroupings(graph, attentionMatrix); |
| 110 | + bool swapOrder = !dimOrdering.empty() && dimOrdering.front().first == 2; |
| 111 | + auto seqMask = |
| 112 | + swapOrder ? popops::sub(graph, seqIndex.dimShuffle({0, 2, 1}), seqIndex, |
| 113 | + prog, "maskVal") |
| 114 | + .dimShuffle({0, 2, 1}) |
| 115 | + : popops::sub(graph, seqIndex, seqIndex.dimShuffle({0, 2, 1}), |
| 116 | + prog, "maskVal"); |
| 117 | + popops::absInPlace(graph, seqMask, prog); |
| 118 | + popops::tanhInPlace(graph, seqMask, prog); |
| 119 | + |
| 120 | + // Create constant tensor; |
| 121 | + std::mt19937 randomEngine; |
| 122 | + unsigned totalTile = graph.getTarget().getTilesPerIPU(); |
| 123 | + std::uniform_int_distribution<> distrib(0, totalTile - 1); |
| 124 | + int tileForConst = distrib(randomEngine); |
| 125 | + poplar::Tensor minValue = graph.addConstant(dataType, {}, -10000.0); |
| 126 | + graph.setTileMapping(minValue, tileForConst); |
| 127 | + |
| 128 | + // Create log mask |
| 129 | + popops::mulInPlace(graph, seqMask, minValue, prog); |
| 130 | + seqMask = seqMask.reshape({batchSize, 1, seqLength, seqLength}); |
| 131 | + setOutTensor(0, seqMask); |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +static popart::popx::OpxCreator<AttentionMaskOpX> attentionMaskOpxCreator( |
| 136 | + CustomOperators::AttentionMask); |
| 137 | + |
| 138 | +static popart::RegisterShapeInferenceFunction AttentionMaskShapeInfer( |
| 139 | + CustomOperators::AttentionMask, [](ShapeInferenceContext& ctx) { |
| 140 | + auto B = ctx.inInfo(1).shape().at(0); |
| 141 | + auto S = ctx.inInfo(1).shape().at(3); |
| 142 | + auto dtype = ctx.inInfo(1).data_type(); |
| 143 | + ctx.outInfo(0) = {dtype, Shape({B, 1, S, S})}; |
| 144 | + }); |
0 commit comments