dlsyscourse / hw3

1 stars 20 forks source link

A bug in ndarray.py #2

Closed RichardLS09 closed 2 years ago

RichardLS09 commented 2 years ago

In hw3/python/needle/backend_ndarray/ndarray.py, Row 299, there is code like this , "start = self.shape[dim]".

In my opinion, it should be "start = self.shape[dim] + start"

navalnica commented 1 year ago

The method looks like:

def process_slice(self, sl, dim):
  """ Convert a slice to an explicit start/stop/step """
  start, stop, step = sl.start, sl.stop, sl.step
  if start == None:
      start = 0
  if start < 0:
      start = self.shape[dim]
  if stop == None:
      stop = self.shape[dim]
  if stop < 0:
      stop = self.shape[dim] + stop
  if step == None:
      step = 1

  # we're not gonna handle negative strides and that kind of thing
  assert stop > start, "Start must be less than stop"
  assert step > 0, "No support for  negative increments"
  return slice(start, stop, step)

I believe the reason we have start = self.shape[dim] line instead of start = self.shape[dim] + start is to ensure that stop < start in case of negative value of start (and to raise AssertionError below)

However it would be nicer to use explicit check instead:

assert start > 0