Skip to content

Move Expected IValue helper methods to common #4212

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions torch_glow/src/GlowIValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,72 @@ size_t GlowIValueMapHash::operator()(const GlowIValue &ival) const {
return 0;
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toDouble,
/// propogate any Errors.
Expected<double> iValToDouble(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toDouble();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toInt,
/// propogate any Errors.
Expected<int64_t> iValToInt(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toInt();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toBool,
/// propogate any Errors.
Expected<bool> iValToBool(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toBool();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toIntList,
/// propogate any Errors.
Expected<std::vector<int64_t> *>
iValToIntList(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toIntList();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toPTTensor,
/// propogate any Errors.
Expected<at::Tensor *> iValToPTTensor(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toPTTensor();
} else {
return expectedIVal.takeError();
}
}

Expected<GlowIValueMap *>
iValToGenericMap(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toGenericMap();
} else {
return expectedIVal.takeError();
}
}

Expected<std::string *> iValToString(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toString();
} else {
return expectedIVal.takeError();
}
}

} // namespace glow
29 changes: 29 additions & 0 deletions torch_glow/src/GlowIValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,35 @@ class GlowIValue {
Error fromIValue(const at::IValue &ival);
};

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toDouble,
/// propogate any Errors.
Expected<double> iValToDouble(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toInt,
/// propogate any Errors.
Expected<int64_t> iValToInt(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toBool,
/// propogate any Errors.
Expected<bool> iValToBool(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toIntList,
/// propogate any Errors.
Expected<std::vector<int64_t> *>
iValToIntList(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toPTTensor,
/// propogate any Errors.
Expected<at::Tensor *> iValToPTTensor(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toGenericMap,
/// propogate any Errors.
Expected<GlowIValueMap *> iValToGenericMap(Expected<GlowIValue *> expectedIVal);

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toString,
/// propogate any Errors.
Expected<std::string *> iValToString(Expected<GlowIValue *> expectedIVal);

} // namespace glow

#endif // GLOW_TORCH_GLOW_SRC_GLOWIVALUE_H
52 changes: 1 addition & 51 deletions torch_glow/src/PyTorchModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,57 +176,6 @@ contractIntIValIfNeeded(Expected<GlowIValue *> expectedGlowIVal) {
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toDouble,
/// propogate any Errors.
Expected<double> iValToDouble(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toDouble();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toInt,
/// propogate any Errors.
Expected<int64_t> iValToInt(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toInt();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toBool,
/// propogate any Errors.
Expected<bool> iValToBool(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toBool();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toIntList,
/// propogate any Errors.
Expected<std::vector<int64_t> *>
iValToIntList(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toIntList();
} else {
return expectedIVal.takeError();
}
}

/// Unwrap a Expected<GlowIValue *> \p expectedIVal and call toPTTensor,
/// propogate any Errors.
Expected<at::Tensor *> iValToPTTensor(Expected<GlowIValue *> expectedIVal) {
if (expectedIVal) {
return (*expectedIVal)->toPTTensor();
} else {
return expectedIVal.takeError();
}
}

/// Given a vector \p original containing elements of some type, \returns a
/// vector of each element cast to another type T.
template <typename T, typename OriginalT>
Expand Down Expand Up @@ -968,6 +917,7 @@ bool PyTorchModelLoader::isNodeSupported(const torch::jit::Node *ptNode) {
}

const auto &mapping = getSymbolsMapping();

return mapping.count(kind) != 0;
}

Expand Down