dmlc / dmlc/dgl

Make errors for invalid arguments/data assertions instead of exceptions.

Open
#3,853 2 comments 0 reactions 0 assignees View on GitHub
RFC
Dominant language
Python
Stars
14.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

## 🚀 Feature
There are many errors/checks in the python code which cannot be gracefully handled, and essentially require program termination (e.g., invalid device inputs), and updating arguments/parameters. Some of these checks require significant overhead.

If we switch these checks to be `assert`s, then users have to option to disable them and see improved performance (when they know their code behaves correctly) via `python -O`.

An example is in `DGLHeteroGraph.find_edges()`:
```
if len(eid) > 0:
min_eid = F.as_scalar(F.min(eid, 0))
if min_eid < 0:
raise DGLError('Invalid edge ID {:d}'.format(min_eid))
max_eid = F.as_scalar(F.max(eid, 0))
if max_eid >= self.num_edges(etype):
raise DGLError('Invalid edge ID {:d}'.format(max_eid))
```
which requires traversing the edge list twice to find the min_eid and the max_eid, and the user is not intended to catch the `DGLError`. Instead this could be:
```
assert len(eid) > 0 or F.as_scalar(F.min(eid, 0)) >= 0, 'Invalid edge ID {:d}'.format(F.as_scalar(F.min(eid, 0)))
assert len(eid) > 0 or F.as_scalar(F.max(eid, 0)) < self.num_edges(etype), 'Invalid edge ID {:d}'.format(F.as_scalar(F.max(eid, 0)))
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.