2#ifndef popfloat_CastToGfloat_hpp
3#define popfloat_CastToGfloat_hpp
5#include <popfloat/experimental/GfloatExpr.hpp>
6#include <popfloat/experimental/GfloatExprUtil.hpp>
7#include <poplar/DebugContext.hpp>
8#include <poplar/Engine.hpp>
9#include <poplar/Graph.hpp>
10#include <poplar/OptionFlags.hpp>
11#include <poplar/Program.hpp>
12#include <poplar/Type.hpp>
54namespace experimental {
58 struct GfloatFormatOptions {
59 unsigned numMantissaBits;
60 unsigned numExponentBits;
61 unsigned numExponentBias;
63 bool enableInfsAndNans;
68 parseGfloatFormatOptions(options);
71 GfloatFormatOptions() {
76 enableInfsAndNans =
true;
81 FormatConfig() =
default;
102 FormatConfig(
unsigned numMantissaBits,
unsigned numExponentBits,
103 int exponentBias,
bool enableDenorms,
bool enableInfsAndNans,
104 popfloat::experimental::SpecType specCalculationType =
105 popfloat::experimental::SpecType::AUTO);
107 FormatConfig(GfloatFormatOptions formatOptions,
110 FormatConfig(
unsigned numMantissaBits,
unsigned numExponentBits,
111 int exponentBias,
bool enableDenorms,
bool enableInfsAndNans,
115 FormatConfig(
const FormatConfig &formatConfig);
117 poplar::Type getCalculationType()
const {
return calculationType; }
118 poplar::Type getNativeType()
const {
return nativeType; }
119 poplar::Type getStorageType()
const {
return storageType; }
121 popfloat::experimental::FormatType getFormatType()
const {
125 unsigned getNumMantissaBits()
const {
return numMantissaBits; }
126 unsigned getNumExponentBits()
const {
return numExponentBits; }
127 int getExponentBias()
const {
return exponentBias; }
128 bool isDenormEnabled()
const {
return enableDenorms; }
129 bool infAndNansEnabled()
const {
return enableInfsAndNans; }
130 bool isPackedFloatFormat()
const {
131 return (formatType !=
132 popfloat::experimental::FormatType::QUANTISED_FP16) &&
134 popfloat::experimental::FormatType::QUANTISED_FP32) &&
135 (formatType != popfloat::experimental::FormatType::IEEE_FP16);
138 unsigned getPackedFloatBits()
const {
return packedFloatBits; };
139 bool isBlockFloat()
const {
return blockFloat; };
141 unsigned getPackedFloatParameters()
const {
return packedFloatParameters; };
143 bool operator==(FormatConfig &other)
const {
144 const auto numMantissaBits_ = other.getNumMantissaBits();
145 const auto numExponentBits_ = other.getNumExponentBits();
146 const auto exponentBias_ = other.getExponentBias();
147 const auto enableDenorms_ = other.isDenormEnabled();
148 const auto enableInfsAndNans_ = other.infAndNansEnabled();
149 const auto calculationType_ = other.getCalculationType();
151 return std::tie(numMantissaBits, numExponentBits, exponentBias,
152 enableDenorms, enableInfsAndNans, calculationType) ==
153 std::tie(numMantissaBits_, numExponentBits_, exponentBias_,
154 enableDenorms_, enableInfsAndNans_, calculationType_);
173 unsigned numMantissaBits;
178 unsigned numExponentBits;
198 bool enableInfsAndNans;
241 popfloat::experimental::FormatType formatType;
253 unsigned packedFloatParameters;
266 unsigned packedFloatBits;
269 struct GfloatCastOptions {
270 popfloat::experimental::RoundType roundMode;
271 popfloat::experimental::SRDensityType srNoiseDensity;
273 double srNoiseOffset;
277 double bernoulliProb;
278 bool enableNanooMode;
283 parseGfloatCastOptions(options);
286 GfloatCastOptions() {
287 roundMode = popfloat::experimental::RoundType::INV;
288 srNoiseDensity = popfloat::experimental::SRDensityType::INVALID;
295 enableNanooMode =
true;
314 RoundConfig() =
default;
316 RoundConfig(popfloat::experimental::RoundType roundMode,
unsigned numSRBits,
318 popfloat::experimental::SRDensityType srNoiseDensity =
319 popfloat::experimental::SRDensityType::INVALID,
320 float srNoiseOffset = 0.0,
float srNoiseScale = 0.0,
321 float srNoiseMax = 0.0,
float srNoiseMin = 0.0,
322 float bernoulliProb = 0.0);
324 RoundConfig(
const GfloatCast::RoundConfig &roundCfg);
326 RoundConfig(GfloatCastOptions castOptions,
poplar::Type calculationType);
328 popfloat::experimental::RoundType getRoundMode()
const {
329 return roundModeType;
332 unsigned getNumSRBits()
const {
return numSRBits; }
334 popfloat::experimental::SRDensityType getSRNoiseDensity()
const {
335 return srNoiseDensity;
337 std::vector<unsigned> getRoundingParams()
const {
return roundingParams; }
338 std::vector<unsigned> getNoiseParams()
const {
return noiseParams; }
339 unsigned getDensityParam()
const {
return densityParam; }
341 float getBernoulliProbability()
const {
return bernoulliProb; }
343 float getSRNoiseOffset()
const {
return srNoiseOffset; }
344 float getSRNoiseScale()
const {
return srNoiseScale; }
345 float getSRNoiseMax()
const {
return srNoiseMax; }
346 float getSRNoiseMin()
const {
return srNoiseMin; }
348 std::vector<unsigned> getSRBitMask()
const {
return srBitMask; }
364 popfloat::experimental::RoundType roundModeType;
403 popfloat::experimental::SRDensityType srNoiseDensity;
488 std::vector<unsigned> noiseParams;
498 unsigned densityParam;
503 std::vector<unsigned> srBitMask;
508 std::vector<unsigned> roundingParams;
524 CastConfig() =
default;
536 createCastNativeToGF(popfloat::experimental::FormatType formatType,
538 RoundConfig roundCfg,
bool enableNanooMode);
551 createCastGFToNative(popfloat::experimental::FormatType formatType,
555 popfloat::experimental::RoundType getRoundMode()
const {
556 return roundConfig.getRoundMode();
559 unsigned getNumSRBits()
const {
return roundConfig.getNumSRBits(); }
561 popfloat::experimental::SRDensityType getSRNoiseDensity()
const {
562 return roundConfig.getSRNoiseDensity();
565 std::vector<unsigned> getNoiseParams()
const {
566 return roundConfig.getNoiseParams();
569 unsigned getDensityParam()
const {
return roundConfig.getDensityParam(); }
571 float getBernoulliProbability()
const {
572 return roundConfig.getBernoulliProbability();
575 float getSRNoiseOffset()
const {
return roundConfig.getSRNoiseOffset(); }
577 float getSRNoiseScale()
const {
return roundConfig.getSRNoiseScale(); }
579 float getSRNoiseMax()
const {
return roundConfig.getSRNoiseMax(); }
581 float getSRNoiseMin()
const {
return roundConfig.getSRNoiseMin(); }
583 std::vector<unsigned> getSRBitMask()
const {
584 return roundConfig.getSRBitMask();
587 bool isNanooModeEnabled()
const {
return enableNanooMode; }
589 std::vector<unsigned> getCastParams()
const {
return castParams; }
591 poplar::Type getCalculationType()
const {
return calculationType; }
592 poplar::Type getStorageType()
const {
return storageType; }
595 return (inType == storageType);
598 popfloat::experimental::FormatType getFormatType()
const {
599 return floatFormatType;
602 bool getStoreAsNative()
const {
return storeAsNative; }
604 std::vector<unsigned> getRoundingParams()
const {
605 return roundConfig.getRoundingParams();
609 CastConfig(popfloat::experimental::FormatType floatFormatType,
611 RoundConfig roundCfg,
bool enableNanooMode);
641 RoundConfig roundConfig;
661 bool enableNanooMode;
678 popfloat::experimental::FormatType floatFormatType;
690 std::vector<unsigned> castParams;
724 GfloatCast(
const FormatConfig &formatCfg,
const RoundConfig &roundCfg,
725 const bool enableNanooMode,
726 const popfloat::experimental::SpecType &GFType =
727 popfloat::experimental::SpecType::AUTO,
728 const popfloat::experimental::SpecType &NativeType =
729 popfloat::experimental::SpecType::AUTO);
731 GfloatCast(
const GfloatFormatOptions &formatOtions,
732 const GfloatCastOptions &castOptions,
poplar::Type calculationType,
733 const popfloat::experimental::SpecType &GFType =
734 popfloat::experimental::SpecType::AUTO,
735 const popfloat::experimental::SpecType &NativeType =
736 popfloat::experimental::SpecType::AUTO);
742 GfloatCast(
const GfloatCast &gfloatCast);
758 const unsigned gfPacked,
869 const CastConfig &gfCastCfg,
898 static void castNativeToGfloatInPlace(
927 static void castNativeToGfloatInPlace(
992 const CastConfig &gfCastCfg,
1014 return nativeToGFCastCfg.getStorageType();
1022 return nativeToGFCastCfg.getCalculationType();
1030 return gfToNativeCastCfg.getStorageType();
1037 FormatConfig getFormatConfig()
const {
return formatCfg; }
1043 CastConfig getNativeToGFConfig()
const {
return nativeToGFCastCfg; }
1049 CastConfig getGFToNativeConfig()
const {
return gfToNativeCastCfg; }
1055 bool getStoreAsNative()
const {
return nativeToGFCastCfg.getStoreAsNative(); }
1067 bool isCastOpParamSet()
const {
return castOpParamSet; }
1073 gfParams.reset(gfParams_);
1076 bool isNanooModeEnabled()
const {
1077 return nativeToGFCastCfg.isNanooModeEnabled();
1080 std::vector<unsigned> getSRBitMask()
const {
1081 return nativeToGFCastCfg.getSRBitMask();
1084 popfloat::experimental::RoundType getRoundMode()
const {
1085 return nativeToGFCastCfg.getRoundMode();
1089 return nativeToGFCastCfg.inPlaceOp(outType);
1092 std::vector<unsigned> getRoundingParams()
const {
1093 return nativeToGFCastCfg.getRoundingParams();
1097 CastConfig nativeToGFCastCfg;
1098 CastConfig gfToNativeCastCfg;
1099 FormatConfig formatCfg;
1100 std::unique_ptr<poplar::Tensor> gfParams;
1101 bool castOpParamSet;
A reference to a compute set within a graph.
Definition: GraphElements.hpp:131
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
A set of option/value string flags to be used in various APIs.
Definition: OptionFlags.hpp:24
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Class representing device data types.
Definition: Type.hpp:42
Program that executes a sequence of programs.
Definition: Program.hpp:77
Define a PopLibs exception.