Skip to content
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

Feature/ python graph API and strongly typed ID #252

Merged
merged 19 commits into from
Mar 7, 2024

Conversation

xinyuli1204
Copy link
Contributor

@xinyuli1204 xinyuli1204 commented Feb 28, 2024

  1. fix duplicate param name error when combine two graphs with same input name.
  2. c_api for graph
    In short words,
    argument_name now becomes std::string s = "_" + node_id + "_" + name + std::to_string(index) + "_" + graph_id;

@xinyuli1204 xinyuli1204 marked this pull request as ready for review February 29, 2024 18:29
@iitaku
Copy link
Collaborator

iitaku commented Feb 29, 2024

@xinyuli1204 Basically looks good.

Now we have three different id (Node, Port and now Graph is added) and it has same type (std::string) even if these are not compatible each other. Can you think about introducing strongly typed alias for each id? ref: https://stackoverflow.com/questions/34287842/strongly-typed-using-and-typedef

int ion_graph_add_node(ion_graph_t, const char*, ion_node_t *);
int ion_graph_destroy(ion_graph_t);
int ion_graph_run(ion_graph_t);
int ion_graph_create_with_multiple(ion_graph_t *, ion_graph_t obj1, ion_graph_t obj2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering more straitforward naming. e.g. ion_graph_create_from_2 ? Do you have any better idea?



template<typename... Args>
std::vector<Port> create_ports_vector(Args... args) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please clarify necessity of the wrapper function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion, this is unnecessary and I delete it

@xinyuli1204 xinyuli1204 marked this pull request as draft March 1, 2024 01:40
@xinyuli1204
Copy link
Contributor Author

@xinyuli1204 Basically looks good.

Now we have three different id (Node, Port and now Graph is added) and it has same type (std::string) even if these are not compatible each other. Can you think about introducing strongly typed alias for each id? ref: https://stackoverflow.com/questions/34287842/strongly-typed-using-and-typedef

@iitaku done
using NodeID = StringID<node_tag>;
using GraphID = StringID<graph_tag>;
using PortID = StringID<port_tag>;

@xinyuli1204 xinyuli1204 marked this pull request as ready for review March 4, 2024 18:18
@xinyuli1204 xinyuli1204 marked this pull request as draft March 4, 2024 18:50
@xinyuli1204 xinyuli1204 changed the title fix/dup-name-error in graph Fix/dup-name-error in graph Mar 4, 2024
@xinyuli1204 xinyuli1204 marked this pull request as ready for review March 4, 2024 19:12
Impl(const std::string& id_, const std::string& name_, const Halide::Target& target_);
Impl(const std::string& id_, const std::string& name_, const Halide::Target& target_, const GraphID &graph_id_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First argument is better to be NodeID, not std::string

@@ -126,6 +131,40 @@ class Node {
{
}

Node(const std::string& id, const std::string& name, const Halide::Target& target, const GraphID& graph_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (NodeID)

@@ -64,12 +65,12 @@ class Port {
std::unordered_map<uint32_t, const void *> instances;

Impl();
Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d);
Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d, const GraphID &gid );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PortID instead of std::string

src/port.cc Outdated
{
params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(pid, pn, 0));
params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(pid, pn, 0, gid.value()));
}

void Port::determine_succ(const std::string& nid, const std::string& old_pn, const std::string& new_pn) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeID instead of std::string

bool has_pred() const { return !std::get<0>(impl_->pred_chan).empty(); }
bool has_pred_by_nid(const std::string& nid) const { return !std::get<0>(impl_->pred_chan).empty(); }
bool has_pred() const { return !std::get<0>(impl_->pred_chan).value().empty(); }
bool has_pred_by_nid(const std::string& nid) const { return !to_string(std::get<0>(impl_->pred_chan)).empty(); }
bool has_succ() const { return !impl_->succ_chans.empty(); }
bool has_succ(const Channel& c) const { return impl_->succ_chans.count(c); }
bool has_succ_by_nid(const std::string& nid) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeID instead of std::string

{
}

Port get_iport(Port arg) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably make_iport is less confusing for this internal API.

@@ -78,58 +79,87 @@ class Port {
* @arg k: The key of the port which should be matched with BuildingBlock Input/Output name.
* @arg t: The type of the value.
*/
Port(const std::string& n, Halide::Type t) : impl_(new Impl("", n, t, 0)), index_(-1) {}
Port(const std::string& n, Halide::Type t) : impl_(new Impl("", n, t, 0, GraphID(""))), index_(-1) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is GraphID("") and GraphID() identical? Let's use either one if there is no meaning for these two use

@@ -220,7 +250,7 @@ class Port {
args.push_back(Halide::Var::implicit(i));
args_expr.push_back(Halide::Var::implicit(i));
}
Halide::Func f(param.type(), param.dimensions(), argument_name(pred_id(), pred_name(), i) + "_im");
Halide::Func f(param.type(), param.dimensions(), argument_name(pred_id(), pred_name(), i, graph_id_to_string()) + "_im");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass GraphID to argument_name directly

@@ -16,7 +16,7 @@ class PortMap {
template<typename T>
[[deprecated("Port::bind can be used instead of PortMap.")]]
void set(Port port, T v) {
auto& buf(scalar_buffer_[argument_name(port.pred_id(), port.pred_name(), port.index())]);
auto& buf(scalar_buffer_[argument_name(port.pred_id_to_string(), port.pred_name(), port.index(), port.graph_id_to_string())]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument name takes NodeID and GraphID, not string

@xinyuli1204
Copy link
Contributor Author

xinyuli1204 commented Mar 5, 2024

@iitaku can I ask something about pred_chan in port.h?
From my understanding pred_chan makes tuple of NodeId and string std::tuple<NodeID, std::string>;, and pred_id() will get previous NodeID , but when constructing new port in port.cc

Port::Impl::Impl(const std::string& pid, const std::string& pn, const Halide::Type& t, int32_t d, const GraphID & gid)
    : id(sole::uuid4().str()), pred_chan{pid, pn}, succ_chans{}, type(t), dimensions(d), graph_id(gid)

pred_chan{pid, pn}, why it takes pid instead?

I wonder if port should init like this

Port::Impl::Impl(const NodeID & nid, const std::string& pn, const Halide::Type& t, int32_t d, const GraphID & gid)
    : id(sole::uuid4().str()), pred_chan{nid, pn}, succ_chans{}, type(t), dimensions(d), graph_id(gid)
{
    params[0] = Halide::Internal::Parameter(type, dimensions != 0, dimensions, argument_name(nid, pn, 0, gid));
}

and is PortId really needed? I didn't find it is used in other place but we can keep it as well

@Fixstars-iizuka
Copy link
Contributor

Fixstars-iizuka commented Mar 6, 2024

@xinyuli1204 It's correct. pid is actually nid.

PortID is not used at this moment, but if it has no downside, let's leave it as it is for the future.

@iitaku iitaku changed the title Fix/dup-name-error in graph Python Graph API / Strongly typed ID Mar 7, 2024
@iitaku iitaku merged commit 245dc1a into main Mar 7, 2024
3 checks passed
@iitaku iitaku mentioned this pull request Mar 7, 2024
@xinyuli1204 xinyuli1204 changed the title Python Graph API / Strongly typed ID Feature/ python graph API and strongly typed ID May 7, 2024
@xinyuli1204 xinyuli1204 deleted the update/python-binding-graph branch November 20, 2024 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants