Skip to content

Conversation

@jl-wynen
Copy link
Member

@jl-wynen jl-wynen commented Oct 5, 2023

Fixes #3253

@jl-wynen jl-wynen requested a review from SimonHeybrock October 5, 2023 14:19
@jl-wynen jl-wynen force-pushed the fix-dtype-event-centric-arithmetic branch from c8d6480 to baefdc9 Compare October 5, 2023 14:29
Comment on lines 121 to 125
map_and_mul_detail::args<float, double, double, double>,
map_and_mul_detail::args<float, double, double, float>,
map_and_mul_detail::args<double, float, float, double>,
map_and_mul_detail::args<double, time_point, time_point, double>,
map_and_mul_detail::args<double, time_point, time_point, float>,
map_and_mul_detail::args<float, time_point, time_point, double>,
map_and_mul_detail::args<float, time_point, time_point, float>>,
map_and_mul_detail::args<float, time_point, time_point, double>>,
Copy link
Member

Choose a reason for hiding this comment

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

Why have some combinations been removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

They dealt with type promotions which are now handled in Python.

Copy link
Member

Choose a reason for hiding this comment

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

What about the inplace variants (which you did not change)?

Copy link
Member Author

Choose a reason for hiding this comment

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

In place operations cannot change the dtype, can they?

Copy link
Member

Choose a reason for hiding this comment

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

No they cannot, but how is that related to the code and my question?

Copy link
Member Author

Choose a reason for hiding this comment

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

Which in-place variants do you actually mean? This is only used by dataset::buckets::scale which is always in-place.

Copy link
Member

Choose a reason for hiding this comment

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

For example __imul__ in bins.py, right under the functions you changed in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

So unless there is some other check, this used to be able to multiply double weights onto float events and convert the doubles to floats. This is no longer possible with this changes.
We allow this in other operations, so I guess I should revert this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

And I actually removed the wrong one for the time_point overload.

def __mul__(self, lut: lookup):
copy = self._obj.copy()
target_dtype = (
scalar(1, dtype=self.constituents['data'].dtype)
Copy link
Member

Choose a reason for hiding this comment

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

I feel we should add self.dtype, similar to self.unit?

Can you check how expensive constituents is? I think it used to unzip the indices (allocating memory), but now it probably just returns 2 stride-2 variables for begin and end, i.e., is cheap?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, self.unit is implemented in terms of constituents. So unless we implement dtype in C++, we'd still pay the cost.
But I agree that it would be nicer to have a dtype property on Bins.

Copy link
Member Author

Choose a reason for hiding this comment

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

And yes, it does unzip the indices.

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure? It looks like it is handled via strides:
image

Copy link
Member Author

@jl-wynen jl-wynen Oct 10, 2023

Choose a reason for hiding this comment

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

I don't know what you want to say with this. But here is the implementation:

template <class T> py::dict bins_constituents(const Variable &var) {
auto &&[indices, dim, buffer] = var.constituents<T>();
auto &&[begin, end] = unzip(indices);
py::dict out;
out["begin"] = std::forward<decltype(begin)>(begin);
out["end"] = std::forward<decltype(end)>(end);
out["dim"] = std::string(dim.name());
out["data"] = std::forward<decltype(buffer)>(buffer);
return out;
}

Copy link
Member

Choose a reason for hiding this comment

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

Probably unzip does this under the hood, i.e., it does not cause copies?

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like it doesn't make a copy: (this messed up the binned variable, but hey ho)

import scipp as sc

da = sc.data.binned_x(10, 3)
print(da.bins.constituents['begin'])
da.bins.constituents['begin'][0] = 1
print(da.bins.constituents['begin'])

->

<scipp.Variable> (x: 3)      int64        <no unit>  [0, 7, 7]
<scipp.Variable> (x: 3)      int64        <no unit>  [1, 7, 7]

Copy link
Member

Choose a reason for hiding this comment

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

👍 ... then this is reasonably fast, i.e., usable for unit and dtype.

@jl-wynen jl-wynen force-pushed the fix-dtype-event-centric-arithmetic branch from 736d98f to e2a26fe Compare October 10, 2023 08:49
@jl-wynen jl-wynen force-pushed the fix-dtype-event-centric-arithmetic branch from e2a26fe to f95556d Compare October 10, 2023 08:50
@jl-wynen jl-wynen enabled auto-merge October 10, 2023 13:37
@jl-wynen jl-wynen merged commit d8197f3 into main Oct 10, 2023
@jl-wynen jl-wynen deleted the fix-dtype-event-centric-arithmetic branch October 10, 2023 13:51
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.

Event-centric binary-operations do not have correct output dtype

3 participants