     dim3 blocks(gradInput->size[0]);
     dim3 threads(LOGSOFTMAX_THREADS);
 
-    cunn_MultiMarginCriterion_updateGradInput_kernel<<<blocks,threads>>>(THCudaTensor_data(state, gradInput),
+    if(p==1)
+      cunn_MultiMarginCriterion_updateGradInput_kernel<1> <<<blocks,threads>>>(THCudaTensor_data(state, gradInput),
+                                                                         THCudaTensor_data(state, input),
+                                                                         THCudaTensor_data(state, target),
+                                                                         gradInput->size[0], gradInput->size[1],
+                                                                         sizeaverage);
+    else if(p==2)
+      cunn_MultiMarginCriterion_updateGradInput_kernel<2> <<<blocks,threads>>>(THCudaTensor_data(state, gradInput),
                                                                          THCudaTensor_data(state, input),
                                                                          THCudaTensor_data(state, target),
                                                                          gradInput->size[0], gradInput->size[1],

    THCudaTensor *output = THCudaTensor_newWithSize1d(state, input->size[0]);
    dim3 blocks(input->size[0]);
    dim3 threads(MULTIMARGIN_THREADS);
    cunn_MultiMarginCriterion_updateOutput_kernel<<<blocks,threads>>>(THCudaTensor_data(state, output),
                                                                      THCudaTensor_data(state, input),
                                                                      THCudaTensor_data(state, target),
                                                                      input->size[0], input->size[1],

{
  THCState *state = getCutorchState(L);
  THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
  int p = luaT_getfieldchecknumber(L, 1, "p");
  int sizeaverage = luaT_getfieldcheckboolean(L, 1, "sizeAverage");

  input = THCudaTensor_newContiguous(state, input);
