-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use custom isnan
for IPU
#490
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Very confusing, but does match the directory structure | ||
from .isnan.isnan import _ipu_isnan as ipu_isnan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CXX ?= g++ | ||
CXXFLAGS = -std=c++14 -fPIC -g | ||
LDLIBS = -shared -lpopart | ||
ONNX_NAMESPACE = -DONNX_NAMESPACE=onnx | ||
|
||
BUILD_DIR = build | ||
SOURCES = isnan_popart.cpp | ||
TARGET = $(BUILD_DIR)/custom_ops.so | ||
|
||
all: create_build_dir isnan_custom_op | ||
|
||
.PHONY: create_build_dir | ||
create_build_dir: | ||
mkdir -p $(BUILD_DIR) | ||
|
||
isnan_custom_op: isnan_popart.cpp | ||
$(CXX) $(SOURCES) $(LDLIBS) $(CXXFLAGS) $(ONNX_NAMESPACE) -o $(TARGET) | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf $(BUILD_DIR) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import torch | ||
import poptorch | ||
import pathlib | ||
import ctypes | ||
|
||
myso = list(pathlib.Path(__file__).parent.rglob("build/cus*.so")) | ||
assert myso, "Failed to find custom op .so file - please cd into `graphium/ipu/isnan` and run `make`" | ||
assert len(myso) == 1, f"Too many ({len(myso)}) custom op .so files, there should only be one" | ||
ctypes.cdll.LoadLibrary(myso[0]) | ||
|
||
def _ipu_isnan(self, x): | ||
|
||
return poptorch.custom_op( | ||
inputs=(x,), | ||
name="IsNanCustom", | ||
domain="custom.ops", | ||
domain_version=1, | ||
example_outputs=(x.bool(),), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2018 Graphcore Ltd. All rights reserved. | ||
#include <poplar/Vertex.hpp> | ||
#include <ipu_builtins.h> | ||
|
||
class IsNaNUsingF32ClassVertex : public poplar::Vertex { | ||
public: | ||
// Fields | ||
poplar::Input<poplar::Vector<float>> in; | ||
poplar::Output<poplar::Vector<bool>> out; | ||
|
||
// Compute function | ||
bool compute() { | ||
|
||
for (int i = 0; i < in.size(); ++i) { | ||
|
||
auto inClass = __builtin_ipu_f32class(in[i]); | ||
|
||
// TFPU_CLASS_UNC = 0 | ||
// TFPU_CLASS_SNAN = 1 | ||
// TFPU_CLASS_QNAN = 2 | ||
// All others are > 3 and not NaN | ||
out[i] = inClass < 3; | ||
|
||
} | ||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) 2020 Graphcore Ltd. All rights reserved. | ||
|
||
#include <popart/opmanager.hpp> | ||
#include <popart/opserialiser.hpp> | ||
#include <popart/popx/opxmanager.hpp> | ||
|
||
#include <popart/popx/opx.hpp> | ||
#include <popops/ElementWise.hpp> | ||
|
||
#include <iostream> | ||
|
||
namespace CustomOperators { | ||
const popart::OperatorIdentifier IsNanId = {"custom.ops", "IsNanCustom", 1}; | ||
} // namespace CustomOperators | ||
|
||
class IsNanOp; | ||
class IsNanOpx; | ||
class IsNanGradOpx; | ||
|
||
class IsNanOp : public popart::Op { | ||
public: | ||
IsNanOp(const popart::OperatorIdentifier &_opid, | ||
const popart::Op::Settings &settings_) | ||
: popart::Op(_opid, settings_) {} | ||
|
||
std::unique_ptr<Op> clone() const final { | ||
return std::make_unique<IsNanOp>(*this); | ||
} | ||
|
||
void setup() final { | ||
outInfo(0) = popart::TensorInfo(popart::DataType::BOOL, | ||
inInfo(0).shape()); | ||
} | ||
|
||
void appendAttributes(popart::OpSerialiserBase &os) const override { | ||
Op::appendAttributes(os); | ||
} | ||
|
||
void appendOutlineAttributes(popart::OpSerialiserBase &os) const override { | ||
Op::appendOutlineAttributes(os); | ||
} | ||
|
||
float getSubgraphValue() const final { return getHighSubgraphValue(); } | ||
|
||
bool requiresRandomSeed() const override { return false; } | ||
|
||
}; | ||
|
||
namespace { | ||
using popart::DataType; | ||
using popart::OpDefinition; | ||
|
||
static OpDefinition isNanOpDef({OpDefinition::Inputs({{"input", {popart::DataType::FLOAT}}}), | ||
OpDefinition::Outputs({{"output", {popart::DataType::BOOL}}}), | ||
OpDefinition::Attributes({}) | ||
}); | ||
|
||
static popart::OpCreator<IsNanOp> isNanOpCreator( | ||
popart::OpDefinitions({{CustomOperators::IsNanId, isNanOpDef}}), | ||
[](const popart::OpCreatorInfo &info) { | ||
return std::make_unique<IsNanOp>(info.opid, info.settings); | ||
}, | ||
true); | ||
} // namespace | ||
|
||
namespace pe = popops::expr; | ||
|
||
class IsNanOpx : public popart::popx::Opx { | ||
public: | ||
IsNanOpx(popart::Op *op, popart::popx::Devicex *devicex) | ||
: popart::popx::Opx(op, devicex) { | ||
verifyOp<IsNanOp>(op, {CustomOperators::IsNanId}); | ||
} | ||
|
||
void grow(poplar::program::Sequence &prog) const final { | ||
|
||
auto op = getOp<IsNanOp>(); | ||
|
||
poplar::Tensor input = getInTensor(0); | ||
auto output = graph().addVariable(poplar::BOOL, input.shape(), debugContext("IsNanCustom")); | ||
auto tileMapping = graph().getTileMapping(input); | ||
graph().setTileMapping(output, tileMapping); | ||
|
||
graph().addCodelets("isnan.gp"); | ||
auto computeSet = graph().addComputeSet("isNanComputeSet"); | ||
|
||
for (unsigned i = 0; i < tileMapping.size(); ++i) { | ||
auto intervals = tileMapping.at(i); | ||
//std::cerr << "i = " << i << std::endl; | ||
//std::cerr << "intervals.size() = " << intervals.size() << std::endl; | ||
for (auto interval : intervals) { | ||
auto vertex = graph().addVertex(computeSet, "IsNaNUsingF32ClassVertex"); | ||
graph().setTileMapping(vertex, i); | ||
graph().connect(vertex["in"], input.flatten().slice(interval.begin(), interval.end())); | ||
graph().connect(vertex["out"], output.flatten().slice(interval.begin(), interval.end())); | ||
} | ||
} | ||
|
||
prog.add(poplar::program::Execute(computeSet)); | ||
|
||
setOutTensor(0, output); | ||
} | ||
}; | ||
|
||
static popart::popx::OpxCreator<IsNanOpx> | ||
IsNanOpxCreator({CustomOperators::IsNanId}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the name of the class ends with
IPU
, this class might be used on CPU or GPU, for reproducibility, debugging, or laziness. So I would recommend creating a new function